package com.jy.adscm.zltx.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.annotation.AdscmLogger;
import com.jy.adscm.bean.system.JyMenu;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.zltx.bean.NewAddQsFile;
import com.jy.adscm.zltx.bean.QsFile;
import com.jy.adscm.zltx.bean.QsFileVersion;
import com.jy.adscm.zltx.bean.QsQsfileDept;
import com.jy.adscm.zltx.service.JyZltxListTreeService;
import com.jy.adscm.zltx.service.QsFileContentService;
import com.jy.adscm.zltx.service.QsFileService;
import com.jy.adscm.zltx.service.QsQsfileDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;



@RestController
@RequestMapping("/adscm/zltx/QsFile")
public class QsFileController {
    @Autowired
    QsFileService qsFileService;
    @Autowired
    QsFileContentService qsFileContentService;
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    JyZltxListTreeService jyZltxListTreeService;
    @Autowired
    QsQsfileDeptService qsQsfileDeptService;



    @RequestMapping("selectByFileId/{fileId}")
    public  SimpleCoreResponse  selectByFileId(@PathVariable  Integer fileId)throws AdscmSystemException{
        QsFile qsFile = qsFileService.selectByFileId(fileId);
        if (qsFile==null){
            throw new AdscmSystemException("联系管理员");
        }
        Wrapper<QsQsfileDept>  wrapper= new EntityWrapper<>();
        wrapper.eq("fileId",fileId);
        List<QsQsfileDept> qsQsfileDepts = qsQsfileDeptService.selectList(wrapper);
        if (qsQsfileDepts.size()>0){
            StringBuilder sb=new StringBuilder();
            List<Integer> FileUseScope= new ArrayList<>();
            for (QsQsfileDept qsQsfileDept : qsQsfileDepts) {
                FileUseScope.add(qsQsfileDept.getDeptId());
            }
            qsFile.setFileUseScope(FileUseScope);
        }
        return  new SimpleCoreResponse("200","查询成功",qsFile);
    }
    @RequestMapping("insert")
    public  SimpleCoreResponse insert(@RequestBody QsFile qsFile , HttpServletRequest request ) throws AdscmSystemException, IOException {
        String tokenId = request.getParameter("tokenId");
        String treeId=request.getParameter("treeId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if (token==null){
            throw  new AdscmSystemException("没找到登录信息");
        }
        qsFile.setFileWriter(token.getUser().getId().toString());
        qsFile.setCreateTime(new Date());
        qsFile.setIsApproval("0");
        boolean insert = qsFileService.insertQsFile(qsFile,treeId);

        List<Integer> fileUseScopes = qsFile.getFileUseScope();
        for (Integer fileUseScope : fileUseScopes) {
            QsQsfileDept qsQsfileDept=new QsQsfileDept();
            qsQsfileDept.setDeptId( fileUseScope );
            qsQsfileDept.setFileId(qsFile.getFileId());
            qsQsfileDeptService.insert(qsQsfileDept);
        }


        return  new SimpleCoreResponse("200","新增成功",insert);
    }
    @RequestMapping("update")
    public  SimpleCoreResponse update(@RequestBody QsFile qsFile , HttpServletRequest request ) throws AdscmSystemException, IOException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if (token==null){
            throw  new AdscmSystemException("没找到登录信息");
        }
        if ("1".equals(qsFile.getIsApproval())){
            qsFile.setFileApprover(token.getUser().getId().toString());
            qsFile.setApprovalTime(new Date());
        }
        boolean insert = qsFileService.updateQsFile(qsFile);
        Wrapper<QsQsfileDept> wrapper= new EntityWrapper<>();
        wrapper.eq("fileId",qsFile.getFileId());
        qsQsfileDeptService.delete(wrapper);
        List<Integer> fileUseScopes = qsFile.getFileUseScope();
        for (Integer fileUseScope : fileUseScopes) {
            QsQsfileDept qsQsfileDept=new QsQsfileDept();
            qsQsfileDept.setDeptId( fileUseScope );
            qsQsfileDept.setFileId(qsFile.getFileId());
            qsQsfileDeptService.insert(qsQsfileDept);
        }


        return  new SimpleCoreResponse("200","新增成功",insert);
    }
    @RequestMapping("/printCurrentVersion/{fileId}")
    public  SimpleCoreResponse printCurrentVersion (@PathVariable Integer fileId,HttpServletRequest request) throws AdscmSystemException{
        NewAddQsFile newAddQsFile = qsFileService.printCurrentVersion(fileId);
        return  new SimpleCoreResponse( "200" ,"查询成功",newAddQsFile);
    }
    @RequestMapping("/historyVersion/{fileId}")
    public  SimpleCoreResponse historyVersion (@PathVariable Integer fileId,HttpServletRequest request) throws AdscmSystemException{
        List<QsFileVersion> qsFileVersions = qsFileService.historyVersion(fileId);
        return  new SimpleCoreResponse( "200" ,"查询成功",qsFileVersions);
    }
    @RequestMapping("/updateHistoryVersion")
    public  SimpleCoreResponse updateQsFileVersion( @RequestBody QsFileVersion qsFileVersion)throws  AdscmSystemException {

        qsFileService.updateQsFileVersion(qsFileVersion);
        return new SimpleCoreResponse("200","修改成功",null);
    }


    @RequestMapping("select")
    public  SimpleCoreResponse select (){
        NewAddQsFile obj= new NewAddQsFile();
        QsFile qsFile =new QsFile();
        qsFile.setFileName("测试数据");
        List<QsQsfileDept> qsQsfileDepts = new ArrayList<>();
        QsQsfileDept QsQsfileDept1=new QsQsfileDept();
        QsQsfileDept1.setDeptId(1);
        qsQsfileDepts.add(QsQsfileDept1);
        QsQsfileDept QsQsfileDept2=new QsQsfileDept();
        QsQsfileDept2.setDeptId(2);
        qsQsfileDepts.add(QsQsfileDept2);
        obj.setQsFile(qsFile);

        return   new SimpleCoreResponse("200","200",obj);

    }

    @AdscmLogger("获取当前用户质量体系菜单下面能操作的资源集合")
    @RequestMapping("/getMenuAllSrc/{menuId}")
    public SimpleCoreResponse getMenuAllSrc(@PathVariable int menuId,HttpServletRequest request) throws AdscmSystemException{


        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId,tokenRedisTemplate);

        List<JyMenu> menus = jyZltxListTreeService.getMenuAllSrc(menuId,token.getUser());

        return new SimpleCoreResponse("200","获取当前用户指定菜单下面能操作的资源集合成功",menus);

    }
    /**
     * 根据菜单ID，获取质量体系目录基本信息
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/selectZltxListTree/{id}")
    public  SimpleCoreResponse  selectZltxListTree (@PathVariable Integer id,HttpServletRequest request)throws AdscmSystemException{
//        String tokenId = request.getParameter("tokenId");
//        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId,tokenRedisTemplate);
//        return new  SimpleCoreResponse("200","查询成功",jyZltxListTreeService.selectAll(id,token.getUser()));

        return new  SimpleCoreResponse("200","查询成功",jyZltxListTreeService.selectListTreeByMenuId(id));
    }
}





